
###############################################################
#    Calculating Prices on Last Login Days 
# This script calculates the prices on the most recent login days.
# When the last login falls on a weekend (Saturday or Sunday), we use the closing prices from the preceding Friday.
###############################################################


anon_in_cleaned<- unique(data.and.login.date.m$anon)
wd_location_logins2 <- as.character("C:/Barclays/raw_data_September_2016/output--logins/output--logins/")
setwd(wd_location_logins2)

login.files <- list.files(pattern=".csv")

library(readr)
d.t.login.files <- as.data.table(parse_number(login.files))
d.t.login.files <- cbind(d.t.login.files, data.table(login.files))
setnames(d.t.login.files, "V1", "anon")
d.t.login.files[, anon:=as.character(anon)]

all_files_w.login <- d.t.login.files[anon %in% anon_in_cleaned] 

data_sample_append<-NULL

for (file_i in 1:nrow(all_files_w.login)) {
  
  anon.login= all_files_w.login[file_i,anon]
  file.login= all_files_w.login[file_i,login.files]
  data.login <- fread(file.login)
  
  data.login[,date.login:=as.Date(d,"%d/%m/%Y" )]
  data.login[,anon:=anon.login ]
  data.login <- unique(data.login[,.(anon, date.login)])
  
  data_sample_append <- rbind(data_sample_append, data.login)
  print(file_i)
}


login.dates<- data_sample_append 
login.dates<- login.dates[order(anon, date.login)]

login.dates[ , last.login:=shift(date.login,1,NA,"lag") ,by=.(anon)] 
login.dates[,anon:=as.numeric(anon)]
setnames(login.dates,"date.login", "port_date")

data.and.login.date.m<-merge(data.and.login.date.m,login.dates, by=c("anon", "port_date"), all.x=T)

data.and.login.date.m[, dow_last_login:=wday(last.login, label=TRUE)]
data.and.login.date.m[dow_last_login=="Sun", last.login:=last.login-2]
data.and.login.date.m[dow_last_login=="Sat", last.login:=last.login-1]
data.and.login.date.m[, dow_last_login:=wday(last.login, label=TRUE)]

dd <- fread("D:/files_moved_19/output datastream/datastream data/310518_prices/datastream_20180601.csv")   

dd[,last.login:=as.Date(date, "%Y-%m-%d")]

dd <- dd[order(Code_used_DS, last.login)]


dd<- dd[Code_used_DS %in% unique(data.and.login.date.m$Code_used_DS)][,.(app,   last.login, Code_used_DS)]
dd<- unique(dd)
setnames(dd, "app", "app_last_login")

data.and.login.date.m<-merge(data.and.login.date.m, dd, by=c("Code_used_DS", "last.login"), all.x=T)

dd<-NULL















